﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VOL;
using BLL;
using System.Data;
using System.Data.SqlClient;

namespace WebShoes.Admin
{
    public partial class HangHoaForm : System.Web.UI.Page
    {
        HangHoaBL _HangHoaBL = new HangHoaBL();
        int MaHangHoa = 0;
        int MaChungLoai = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();

                string _MaHang = Request.QueryString["MaHangHoa"];

                if (_MaHang == null || _MaHang == "" || !int.TryParse(_MaHang, out MaHangHoa))
                {
                    hdfMaHangHoa.Value = "0";
                }
                else
                {
                    
                    DataTable dt = _HangHoaBL.searchIDHangHoa(MaHangHoa);
                    txtTenHangHoa.Text = dt.Rows[0]["TenHangHoa"].ToString();
                    txtSoLuong.Text = dt.Rows[0]["SoLuongHienCo"].ToString();
                    txtKhuyenMai.Text = dt.Rows[0]["KhuyenMai"].ToString();
                    txtDonGia.Text = dt.Rows[0]["DonGia"].ToString();
                    txtChiTiet.Text = dt.Rows[0]["ChiTiet"].ToString();
                    hdfMaHangHoa.Value = _MaHang;
                    ddlChungLoai.SelectedValue = dt.Rows[0]["MaChungLoai"].ToString();
                    
                    loadSize();
                    ddlKichCo.SelectedValue = dt.Rows[0]["KichCo"].ToString();
                    
                    ddlLoaiHangHoa.SelectedValue = dt.Rows[0]["MaLoaiHangHoa"].ToString();
                    ddlNhaSanXuat.SelectedValue = dt.Rows[0]["MaNhaSanXuat"].ToString();
                    Image1.ImageUrl = dt.Rows[0]["HinhAnh"].ToString();

                    

                    //obj.MaHang = MaHang;
                    //DataTable dt = _HangHoaBL.searchIDHangHoa(obj);
                    //txtTenHang.Text = dt.Rows[0]["TenHang"].ToString();
                    //txtMoTa.Text = dt.Rows[0]["MoTa"].ToString();
                    //txtDVT.Text = dt.Rows[0]["DVT"].ToString();
                    //txtDonGia.Text = dt.Rows[0]["DonGia"].ToString();
                    //hdfMaHang.Value = _MaHang;
                    //ddlLoaiHang.SelectedValue = dt.Rows[0]["MaLoaiHang"].ToString();
                    //ddlNhaCungCap.SelectedValue = dt.Rows[0]["MaNhaCungCap"].ToString();
                    //Image1.ImageUrl = dt.Rows[0]["HinhAnh"].ToString();
                }
            }
        }

        private void FillData()
        {

            lblThongBao.Visible = false;
            LoaiHangHoaBL _loaiHangHoaBL = new LoaiHangHoaBL();
            ddlLoaiHangHoa.DataSource = _loaiHangHoaBL.GetData();
            ddlLoaiHangHoa.DataTextField = "TenLoaiHangHoa";
            ddlLoaiHangHoa.DataValueField = "MaLoaiHangHoa";
            ddlLoaiHangHoa.DataBind();
            ddlLoaiHangHoa.Items.Insert(0, "--Vui lòng Chọn--");

            ChungLoaiBL _chungLoaiBL = new ChungLoaiBL();
            ddlChungLoai.DataSource = _chungLoaiBL.GetData();
            ddlChungLoai.DataTextField = "TenChungLoai";
            ddlChungLoai.DataValueField = "MaChungLoai";
            ddlChungLoai.DataBind();
            ddlChungLoai.Items.Insert(0, "--Vui lòng Chọn--");

            NhaSanXuatBL _nhaSanXuatBL = new NhaSanXuatBL();
            ddlNhaSanXuat.DataSource = _nhaSanXuatBL.GetData();
            ddlNhaSanXuat.DataTextField = "TenNhaSanXuat";
            ddlNhaSanXuat.DataValueField = "MaNhaSanXuat";
            ddlNhaSanXuat.DataBind();
            ddlNhaSanXuat.Items.Insert(0, "--Vui lòng Chọn--");

            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtDonGia.Text == "" || txtKhuyenMai.Text == "" || txtSoLuong.Text == "" || ddlChungLoai.SelectedIndex == 0
                || ddlKichCo.SelectedIndex == 0 || ddlLoaiHangHoa.SelectedIndex == 0 || ddlNhaSanXuat.SelectedIndex ==0)
            {
                lblThongBao.Visible = true;
                lblThongBao.Text = "Mời nhập đầy đủ thông tin hàng hóa";
                return;
            }

            string _MaHang = hdfMaHangHoa.Value;

            string path = "~/images/";
            string imageFile = Image1.ImageUrl.ToString();

            if (FileUpload1.HasFile)
            {
                imageFile = path + FileUpload1.FileName;
                FileUpload1.SaveAs(Server.MapPath(imageFile));
            }

            if (_MaHang == null || _MaHang == "0" || _MaHang == "")
            {
                // thực hiện thêm mới
                HangHoa obj = new HangHoa();
                obj.ChiTiet = txtChiTiet.Text;
                obj.DonGia = float.Parse(txtDonGia.Text);
                obj.KhuyenMai = int.Parse(txtKhuyenMai.Text);
                obj.KichCo = int.Parse(ddlKichCo.SelectedValue);
                obj.NgayNhap = DateTime.Now;
                obj.SoLuongHienCo = int.Parse(txtSoLuong.Text);
                obj.TenHangHoa = txtTenHangHoa.Text;
                obj.HinhAnh = imageFile;
                obj.MaChungLoai = int.Parse(ddlChungLoai.SelectedValue);
                obj.MaLoaiHangHoa = int.Parse(ddlLoaiHangHoa.SelectedValue);
                obj.MaNhaSanXuat = int.Parse(ddlNhaSanXuat.SelectedValue);

                _HangHoaBL.insertHangHoa(obj);
            }
            else
            {
                // thực hiện sửa
                HangHoa obj = new HangHoa();
                obj.MaHangHoa = int.Parse(_MaHang);
                obj.ChiTiet = txtChiTiet.Text;
                obj.DonGia = float.Parse(txtDonGia.Text);
                obj.KhuyenMai = int.Parse(txtKhuyenMai.Text);
                obj.KichCo = int.Parse(ddlKichCo.SelectedValue);
                obj.NgayNhap = DateTime.Now;
                obj.SoLuongHienCo = int.Parse(txtSoLuong.Text);
                obj.TenHangHoa = txtTenHangHoa.Text;
                obj.HinhAnh = imageFile;
                obj.MaChungLoai = int.Parse(ddlChungLoai.SelectedValue);
                obj.MaLoaiHangHoa = int.Parse(ddlLoaiHangHoa.SelectedValue);
                obj.MaNhaSanXuat = int.Parse(ddlNhaSanXuat.SelectedValue);

                _HangHoaBL.updateHangHoa(obj);
            }

            Response.Redirect("~/Admin/HangHoaManagerForm.aspx");
        }

        private void loadSize()
        {
            try
            {
                MaChungLoai = int.Parse(ddlChungLoai.SelectedValue);

            }
            catch (Exception ex) { }
            ddlKichCo.Items.Clear();
            if (MaChungLoai == 0) return;
            if (MaChungLoai == 3 || MaChungLoai == 5)
            {
                int i = 0;
                for (i = 10; i <= 25; i++)
                {
                    ddlKichCo.Items.Add(i.ToString());

                }

            }
            if (MaChungLoai == 1 || MaChungLoai == 2)
            {
                int i = 0;
                for (i = 25; i <= 45; i++)
                {
                    ddlKichCo.Items.Add(i.ToString());

                }
            }
            ddlKichCo.Items.Insert(0, "--Vui lòng Chọn--");
        }

        protected void ddlChungLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadSize();
            //ddlChungLoai.SelectedValue = MaChungLoai.ToString();
        }
    }
}